package com.example.kyyl.utils;

import android.annotation.SuppressLint;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtil {
    public static final String DATETIME_DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_DEFAULT_FORMAT = "yyyy-MM-dd";
    public static final String DAY_DEFAULT_FORMAT = "dd";
    public static final String TIME_DEFAULT_FORMAT = "HH:mm:ss";
    private static DateFormat dateFormat = new SimpleDateFormat(DATE_DEFAULT_FORMAT);
    private static DateFormat dateTimeFormat = new SimpleDateFormat(DATETIME_DEFAULT_FORMAT);
    private static Calendar gregorianCalendar = new GregorianCalendar();
    private static DateFormat timeFormat = new SimpleDateFormat(TIME_DEFAULT_FORMAT);

    public static Date formatDate(String str, String str2) {
        try {
            return new SimpleDateFormat(str2).parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getDateFormat(Date date) {
        return dateFormat.format(date);
    }

    public static String getDateTimeFormat(Date date) {
        return dateTimeFormat.format(date);
    }

    public static String getTimeFormat(Date date) {
        return timeFormat.format(date);
    }

    public static String getDateFormat(Date date, String str) {
        if (str != null) {
            return new SimpleDateFormat(str).format(date);
        }
        return null;
    }

    public static Date getDateFormat(String str) {
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date getDateTimeFormat(String str) {
        try {
            return dateTimeFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date getNowDate() {
        return getDateFormat(dateTimeFormat.format(new Date()));
    }

    @SuppressLint("WrongConstant")
    public static Date getFirstDayOfWeek() {
        gregorianCalendar.setFirstDayOfWeek(2);
        gregorianCalendar.setTime(new Date());
        gregorianCalendar.set(7, gregorianCalendar.getFirstDayOfWeek());
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getLastDayOfWeek() {
        gregorianCalendar.setFirstDayOfWeek(2);
        gregorianCalendar.setTime(new Date());
        gregorianCalendar.set(7, gregorianCalendar.getFirstDayOfWeek() + 6);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getFirstDayOfWeek(Date date) {
        if (date == null) {
            return null;
        }
        gregorianCalendar.setFirstDayOfWeek(2);
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(7, gregorianCalendar.getFirstDayOfWeek());
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getLastDayOfWeek(Date date) {
        if (date == null) {
            return null;
        }
        gregorianCalendar.setFirstDayOfWeek(2);
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(7, gregorianCalendar.getFirstDayOfWeek() + 6);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getFirstDayOfMonth() {
        gregorianCalendar.setTime(new Date());
        gregorianCalendar.set(5, 1);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getLastDayOfMonth() {
        gregorianCalendar.setTime(new Date());
        gregorianCalendar.set(5, 1);
        gregorianCalendar.add(2, 1);
        gregorianCalendar.add(5, -1);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getFirstDayOfMonth(Date date) {
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(5, 1);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getLastDayOfMonth(Date date) {
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(5, 1);
        gregorianCalendar.add(2, 1);
        gregorianCalendar.add(5, -1);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getDayBefore(Date date) {
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(5, gregorianCalendar.get(5) - 1);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static Date getDayAfter(Date date) {
        gregorianCalendar.setTime(date);
        gregorianCalendar.set(5, gregorianCalendar.get(5) + 1);
        return gregorianCalendar.getTime();
    }

    @SuppressLint("WrongConstant")
    public static int getNowYear() {
        return Calendar.getInstance().get(1);
    }

    @SuppressLint("WrongConstant")
    public static int getNowMonth() {
        return Calendar.getInstance().get(2) + 1;
    }

    @SuppressLint("WrongConstant")
    public static int getNowMonthDay() {
        return Calendar.getInstance().getActualMaximum(5);
    }

    @SuppressLint("WrongConstant")
    public static List<Date> getEveryDay(Date date, Date date2) {
        if (date == null || date2 == null) {
            return null;
        }
        Date dateFormat2 = getDateFormat(getDateFormat(date));
        Date dateFormat3 = getDateFormat(getDateFormat(date2));
        ArrayList arrayList = new ArrayList();
        gregorianCalendar.setTime(dateFormat2);
        arrayList.add(gregorianCalendar.getTime());
        while (gregorianCalendar.getTime().compareTo(dateFormat3) < 0) {
            gregorianCalendar.add(5, 1);
            arrayList.add(gregorianCalendar.getTime());
        }
        return arrayList;
    }

    @SuppressLint("WrongConstant")
    public static Date getFirstMonth(int i) {
        Calendar instance = Calendar.getInstance();
        instance.add(2, -i);
        return instance.getTime();
    }
}
